const enterpriseVue = new Vue({
  el: '#app',
  data() {
    return {
      pickerOptions: pickerOptions,
      rules: {
        name: [{
          required: true,
          message: '请输入栏目名称',
          trigger: 'blur',
        }],
        src: [{
          required: true,
          message: '请输入图片地址',
          trigger: 'blur',
        }],
        url: [{
          required: true,
          message: '请输入链接地址',
          trigger: 'blur',
        }],
        zone: [{
          required: true,
          message: '请选择所属区域',
          trigger: 'change'
        }]
      },
      fileList2: [{ name: 'food.jpeg', url: '../../../../admin/images/act.jpg' }],
      dialogImageUrl: '',
      dialogVisible: false,
      form: {
        ShopColumn_Id: '',
        name: '',
        src: '',
        url: '',
        App_ID: 'wx752cd7c5466abbaf',    //小程序appid
        Enterprise_Info_SerialNumber: $.cookie("enterprise"),
        AppID_Path_Type: "0"
      },
      src:'',
      formData: {
        Data: "Shop_Column",
        Primary_Key: "",
        Primary_Key_Name: "ShopColumn_Id",
      },
      loading: false,
      AddressList: [],
    }
  },
  mounted() {
    this.formData.Primary_Key = sessionStorage.getItem("ShopColumn_Id");
    console.log(sessionStorage.getItem("ShopColumn_Id"));
    this.getAddress();
    this.get_original_value(this.formData)
  },
  methods: {
    //上传图片
    uploadFront(file) {
      let _this = this
      new Compressor(file, {
        quality: 1,
        // if(file.size) {
        // }
        success(file) {
          console.log(file.size)
          const formData = new FormData();
          formData.append('file', file, file.name);
          let oFileReader = new FileReader();
          oFileReader.readAsDataURL(file);
          oFileReader.onload = function (e) {
            _this.src = e.target.result
            $.ajax({
              type: 'POST',
              url: DomainName + '/FileListColumn',
              data: {
                Data: e.target.result,
                DataLength: file.size,
                FileName: file.name
              },
              complete: function (XMLHttpRequest) {
                _this.loading = false
                if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                  window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                }
              },
              success: function (Result) {
                if (Result.Status == "success") {
                  _this.form.src = Result.Picture_Url
                  _this.$message(Result.Picture_Url)
                } else {
                  _this.$message.error(Result.msg)
                }
              },
              error: function () {
                _this.$message.error("操作失败，请稍后再试")
              },
              dataType: "json"
            });
          }
        },
        error(err) {
          console.log(err.message);
        },
      });
    },
    handlePictureCardPreview(file) {
      this.dialogVisible = true
      this.dialogImageUrl = file
    },
    // 删除图片
    handleRemove() {
      this.form.src = ''
    },
    //查询原值
    get_original_value(data) {
      let _this = this
      $.ajax({
        url: DomainName + '/getInquiryIDOriginal?token=' + $.cookie('token'),
        type: 'POST',
        data,
        success: function (res) {
          console.log(res);
          _this.form = res[0];
          console.log(_this.form);
        },
        complete: function (XMLHttpRequest) {
          _this.loading = false
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        }
      })
    },
    //查询路径
    getAddress() {
      const _this = this
      $.ajax({
        type: 'POST',
        url: DomainName + '/getInquiryAction?token=' + $.cookie("token"),//
        data: {
          Data: "enterprise_appid_path",
          isNullEnterprise: true
        },
        complete: function (XMLHttpRequest) {
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        },
        success: function (Result) {
          _this.AddressList = Result;
        },
        dataType: "json"
      });
    },
    submitForm(formName) {
      let _this = this
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const { add, modify } = _this.deal()
          let data = {}
          if (add == null) {
            data = { Modify: JSON.stringify({ Shop_Column: [modify] }) }
          } else {
            data = {
              Modify: JSON.stringify({ Shop_Column: [modify] }),
              Add: JSON.stringify({ Shop_Column: [add] })
            }
          }
          _this.loading = true
          console.log(data);
          $.ajax({
            // url: 'https://u345171z68.imdo.co/ShopColumnEdit',
            url: DomainName + '/ShopColumnEdit',
            type: 'POST',
            data: data,
            success: function (res) {
              console.log('提交结果', res)
              _this.$message.success("提交成功")
              window.location.href = './Classified.html'
              // window.location.href = './lanmu.html'
            },
            error: function () {
              _this.$message.error("操作失败，请稍后再试")
            },
            complete: function (XMLHttpRequest) {
              _this.loading = false
              if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                window.parent.document.location.href = "../../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
              }
            }
          })
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    deal() {
      const modify = deepClone(this.form)
      return { add: null, modify };
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  }
})